package com.gzhryc.socket;

import java.util.List;
import java.util.Properties;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.gzhryc.common.config.PropertyTools;

public class SocketConfig {

	protected String type; // 配置类型，Normal，MQTT

	// Socket
	protected String ip = "127.0.0.1";
	protected Integer port;
	protected boolean ssl = false;

	protected byte[] endSymbol; // 结束符，避免沾包
	protected Integer maxWaitBufferSize = 8912; // 最大等待缓冲区大小，仅在设计结束符时有用

	// MQTT
	protected String clientId;
	protected String protocol = "tcp";
	protected String userName;
	protected String userPwd;
	protected String fileDir; // 本地缓存目录
	protected boolean cleanSession = true; // 是否清除缓冲
	protected Integer keepAliveInterval = 60; // 会话心跳时间（秒）
	protected Integer connectionTimeout = 30; // 超时时间（秒）
	protected String testTopic;
	protected List<String> subscribeTopicList;

	//通用
	protected Long waitTime; // MQTT为等待时间，TCP为超时时间，单位：秒

	protected Integer corePoolSize = 3;			//线程池核心线程数
	protected Integer maximumPoolSize = 30;		//线程池最大线程数
	protected Integer keepAliveTime = 180;		//线程空闲时间，单位：秒

	protected Integer reconnectInterval = 60; // 重连间隔
	protected TimeUnit unit = TimeUnit.SECONDS;

	public SocketConfig() {
	}

	public SocketConfig(String type) {
		this.type = type;
	}

	public SocketConfig(Properties properties) {
		PropertyTools.setObject(properties, this);
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public Integer getPort() {
		return port;
	}

	public void setPort(Integer port) {
		this.port = port;
	}

	public boolean isSsl() {
		return ssl;
	}

	public void setSsl(boolean ssl) {
		this.ssl = ssl;
	}

	public Integer getCorePoolSize() {
		return corePoolSize;
	}

	public void setCorePoolSize(Integer corePoolSize) {
		this.corePoolSize = corePoolSize;
	}

	public Integer getMaximumPoolSize() {
		return maximumPoolSize;
	}

	public void setMaximumPoolSize(Integer maximumPoolSize) {
		this.maximumPoolSize = maximumPoolSize;
	}

	public Integer getKeepAliveTime() {
		return keepAliveTime;
	}

	public void setKeepAliveTime(Integer keepAliveTime) {
		this.keepAliveTime = keepAliveTime;
	}

	public Integer getKeepAliveInterval() {
		return keepAliveInterval;
	}

	public void setKeepAliveInterval(Integer keepAliveInterval) {
		this.keepAliveInterval = keepAliveInterval;
	}

	public String getTestTopic() {
		return testTopic;
	}

	public void setTestTopic(String testTopic) {
		this.testTopic = testTopic;
	}

	public List<String> getSubscribeTopicList() {
		return subscribeTopicList;
	}

	public void setSubscribeTopicList(List<String> subscribeTopicList) {
		this.subscribeTopicList = subscribeTopicList;
	}

	public Integer getConnectionTimeout() {
		return connectionTimeout;
	}

	public void setConnectionTimeout(Integer connectionTimeout) {
		this.connectionTimeout = connectionTimeout;
	}

	public TimeUnit getUnit() {
		return unit;
	}

	public void setUnit(TimeUnit unit) {
		this.unit = unit;
	}

	public String getClientId() {
		if (this.clientId == null) {
			this.clientId = UUID.randomUUID().toString().replace("-", "");
		}
		return clientId;
	}

	public void setClientId(String clientId) {
		this.clientId = clientId;
	}

	public String getProtocol() {
		return protocol;
	}

	public void setProtocol(String protocol) {
		this.protocol = protocol;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getUserPwd() {
		return userPwd;
	}

	public void setUserPwd(String userPwd) {
		this.userPwd = userPwd;
	}

	public Long getWaitTime() {
		return waitTime;
	}

	public void setWaitTime(Long waitTime) {
		this.waitTime = waitTime;
	}

	public String getFileDir() {
		return fileDir;
	}

	public void setFileDir(String fileDir) {
		this.fileDir = fileDir;
	}

	public boolean isCleanSession() {
		return cleanSession;
	}

	public void setCleanSession(boolean cleanSession) {
		this.cleanSession = cleanSession;
	}

	public Integer getReconnectInterval() {
		return reconnectInterval;
	}

	public void setReconnectInterval(Integer reconnectInterval) {
		this.reconnectInterval = reconnectInterval;
	}

	public byte[] getEndSymbol() {
		return endSymbol;
	}

	public void setEndSymbol(byte[] endSymbol) {
		this.endSymbol = endSymbol;
	}

	public Integer getMaxWaitBufferSize() {
		return maxWaitBufferSize;
	}

	public void setMaxWaitBufferSize(Integer maxWaitBufferSize) {
		this.maxWaitBufferSize = maxWaitBufferSize;
	}

	public String getUrl() {
		return protocol + "://" + ip + ":" + port;
	}

	public void setUrl(String url) {
		Pattern pat = Pattern.compile("^([^:]*)://([^:]*):(\\d*)$");
		Matcher mat = pat.matcher(url);
		if (mat.find()) {
			this.protocol = mat.group(1);
			this.ip = mat.group(2);
			this.port = Integer.valueOf(mat.group(3));
		}
	}
}
